package Cart_Function;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import Cart_Utils.Cart;
import JDBC.JDpoor;

public class SetOrder_Function {
	
	Connection con = new JDpoor().CreateCon();;
	Statement sql = null;
	PreparedStatement pre = null;
	ResultSet rs = null;
	boolean f = false;
	
	public void SetOrder(ArrayList<Cart> carts) {
		// TODO Auto-generated method stub
		String condition = "insert into order_info values(?,?,?,?,?)";
		if(!isHasOrder(carts.get(0).getUser())){
			for(int i=0;i<carts.size();i++){
				if(new Cart_Function().hasStock(carts.get(i).getName(), carts.get(i).getNum())){
					try {
						pre = con.prepareStatement(condition);
						pre.setString(1,carts.get(i).getName());
						pre.setString(2,carts.get(i).getNum());
						pre.setString(3,carts.get(i).getPrice());
						pre.setString(4,carts.get(i).getTotal());
						pre.setString(5,carts.get(i).getUser());
						pre.executeUpdate();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					updateStock(carts.get(i));
				}
			}
		}
	}

	private boolean isHasOrder(String user) {
		// TODO Auto-generated method stub
		String condition = "select * from order_info where user=?";
		try {
			pre = con.prepareStatement(condition);
			pre.setString(1, user);
			rs = pre.executeQuery();
			if(rs.next()){
				f = true;
			}else
				f = false;
		} catch (Exception e) {
			// TODO: handle exception
			f = true;
			System.out.println(e);
		}
		return f;
	}

	private void updateStock(Cart cart) {
		// TODO Auto-generated method stub
		String condition = "update goods_info set stock=? where name=?";
		String stock = getNewStock(cart);
		try {
			pre = con.prepareStatement(condition);
			pre.setString(1, stock);
			pre.setString(2, cart.getName());
			pre.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	private String getNewStock(Cart cart) {
		// TODO Auto-generated method stub
		String condition = "select stock from goods_info where name=?";
		String stock = null;
		try {
			pre = con.prepareStatement(condition);
			pre.setString(1, cart.getName());
			rs = pre.executeQuery();
			while(rs.next()){
				stock = Integer.parseInt(rs.getString("stock"))-Integer.parseInt(cart.getNum())+"";
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
		}
		return stock;
	}

	public ArrayList<Cart> getOrder(String user) {
		// TODO Auto-generated method stub
		ArrayList<Cart> carts = new ArrayList<Cart>();
		String condition = "select * from order_info where user=?";
		
		try {
			pre = con.prepareStatement(condition);
			pre.setString(1, user);
			rs = pre.executeQuery();
			while(rs.next()){
				Cart cart = new Cart();
				cart.setName(rs.getString("name"));
				cart.setNum(rs.getString("num"));
				cart.setPrice(rs.getString("price"));
				cart.setTotal(rs.getString("total"));
				cart.setUser(rs.getString("user"));
				carts.add(cart);
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
		}
		return carts;
	}

}
